//<script type="text/javascript" src="../jquery-event/jquery-1.4.2.min.js" charset="utf-8"></script>

//para ver si un formulario o campo es valido, una vez se envía
$(document).ready(function(){
alert(" name Valid: " + $("#regist").valid());
alert(" name Valid: " + $("#name").valid());
alert(" papell Valid: " + $("#papell").valid());
});

//efectos de foco
$(document).ready(function(){
	$("#ciudad").focus(function(){
		// $('#my_image').remove(); 

 	});
});
//un intento por validar en tiempo real :(
//funcion para verificar en tiempo real que los campos requeridos no estén vacios
function checkLive( campo, valor ){
	if( valor == 'domjudge-must-select' || valor == '' ){
		$(campo).attr("src","../images/s_error.png");   
		}else{
			$(campo).attr("src","../images/s_okay.png");
		}
}
$(document).ready(function(){
	
	$("#ciudad").blur(function(event){
		var campo = "#my_image";
		var valor = $("#ciudad").find(':selected').val();
		checkLive( campo, valor );
	});
	$("#name").blur(function(event){
		var campo = "#image_name";
		var valor = $("#name").val();
		checkLive( campo, valor );
	});
	$("#ciudad").focus(function(){
		 $('#checkcity').remove(); 

 	});
	$("#ciudad").blur(function(event){
		var valor = $("#ciudad").find(':selected').val();
		if( valor == 'domjudge-must-select'){
			$("#checkcity").append('Select a city');    
		}
	});});

//otra forma de malipular los valores con valid()
$(document).ready(function(){
	$("#ciudad").change(function(event){
		var id = $("#ciudad").find(':selected').val();
		if( id == '' ){
			return $("#ciudad").valid();
		}	
		$("#instit").load('genera-select.php?id='+id);
		
	});});
//validar existencia de usuario
$(document).ready(function(){	
	$("#user").blur(function(event){
		user = $(this).val(),
		$.post("valid_user.php", { user: user }, function(data){
				//$("#combo2").html(data);
				//$("#combo3").html("");
				//alert(data);
				if ( data == 'no' ){
					//$("#usernameResponse").css( 'display', 'inline' );
					//alert(data);
					$("#usernameResponse").text(data);
					
				}

				
			});
		//alert(nick);
		//$( "#usernameResponse" ).css( 'display', 'inline' );
	});
});
//**
$(document).ready(function()
{
    $("#username").blur(function()
    {
    //remove all the class add the messagebox classes and start fading
    $("#msgbox2").removeClass().addClass('messagebox').text('Checking...').fadeIn("slow");
    //check the username exists or not from ajax
    $.post("checkusername.php",{ username:$(this).val() } ,function(data)
    {
      if(data=='no') //if username not avaiable
      { 
        $("#msgbox2").fadeTo(200,0.1,function() //start fading the messagebox
        {//alert(data);
          //add message and change the class of the box and start fading
          $(this).html('Your username was taken<? $_SESSION['test2'] = 0; ?>').addClass('messageboxerror') .fadeTo(900,1);
        });     
      }
      else 
      {
        $("#msgbox2").fadeTo(200,0.1,function()  //start fading the messagebox
        { //alert(data);
          //add message and change the class of the box and start fading
          $(this).html('User name is available!<? $_SESSION['test2'] = 1; ?>').addClass('messageboxok').fadeTo(900,1);  
        });


      }

    });

});
});

//no me funciona es la version más pulida :(
$(document).ready(function(){	
	$("#user").blur(function(event){
	//$("#usernameResponse").removeClass().addClass('messagebox').text('Checking...').fadeIn("slow");
		$.post("valid_user.php", { user: $(this).val() }, function(datos){
			var res= $( datos ).value();
		alert(res);
		 if(datos=='si'){ 
        	$("#usernameResponse").fadeTo(200,0.1,function(){
		 alert(datos);
         	 //add message and change the class of the box and start fading
          	 $(this).html('Your username was taken').addClass('messageboxerror') .fadeTo(900,1);
        	});
		}
		if(datos=='no'){
        	$("#usernameResponse").fadeTo(200,0.1,function(){ 
		alert(datos);
         	//add message and change the class of the box and start fading
          	$(this).html('User name is available!').addClass('messageboxok').fadeTo(900,1);  
        	});}
		});
		});
});
//***************************************************************
$(document).ready(function(){	
	$("#user").blur(function(event){
	//$("#usernameResponse").removeClass().addClass('messagebox').text('Checking...').fadeIn("slow");
		$.post("valid_user.php", { user: $(this).val() }, function(datos){
				$("#usernameResponse").text(datos);
				//var recuperar = $("#usernameResponse").text();
				alert(datos);
				/*if( recuperar=='si' ){
				$("#usernameResponse").fadeTo(200,0.1,function(){
		 		//alert(recuperar);
         	 		//add message and change the class of the box and start fading
			//$("#usernameResponse").removeClass('respuesta');	
          	 	//$(this).html('Your username was taken').addClass('messageboxerror') .fadeTo(900,1);
        		//});*/
				}
			//alert(datos);
		});
});
